home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10997 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  77 lines

  1. Path: news.ner.bbnplanet.net!forest!groy
  2. From: groy@forest.drew.edu (I can't think of a nickname)
  3. Newsgroups: comp.lang.c
  4. Subject: HELPP!!! --very important-- #2
  5. Message-ID: <1996Mar21.001307.138164@forest>
  6. Date: 21 Mar 96 00:13:07 EST
  7. Organization: Drew University
  8.  
  9. Hello once again fellow programmers.. here is another program problem..
  10. It's supposed to print out the prime numbers from 1 to 1000..(I took this 
  11. from a book) but multiples of 3,and 5 keep comming up.. help??
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <math.h>
  16. #define NMAX 1000
  17.  
  18.  
  19.  
  20. int main (void)  
  21.  
  22.  
  23.  int n = 1,
  24.      min_div, /*minimum divisor (greater than 1) of n */
  25.      step = 1;
  26.     printf ("%i ",n);  /* prints out 1.. thew first prime number */
  27.  for(n=2; n <= NMAX; n++)  
  28.    min_div = find_div(n);
  29.    if (min_div == n) 
  30.         printf("%i ", n); step++;
  31.         if (step == 20) 
  32.                printf("\n"); step =0;   /*end of if min_div == n */
  33.     /* end for loop */
  34.   printf("\n"); /*nesisary carriage return (sorry about my spelling) */
  35.  /* end main */
  36. int find_div(int n) 
  37.  
  38.      int trial,  /* current value of n */
  39.          divisor; /* smallest divisor of n; zero means that divisor not 
  40. yet found */
  41.      int even(int num);
  42.  
  43.      if (even(n)) 
  44.           divisor = 2;
  45.       else 
  46.              divisor =0;
  47.              trial = 3;
  48.      
  49.      while (divisor == 0)   /* finds is there is any possible int that 
  50. would kepp it from being a prime number */
  51.          if (trial > sqrt(n))
  52.                 divisor = n;
  53.          else if (( n % trial) == 0)
  54.                 divisor = trial;
  55.          else
  56.                 trial = trial + 2;
  57.      
  58.  
  59.   return (divisor);
  60.  /* end find_div */
  61.  
  62. int even(int num)    /* finds is num is even or not */
  63.     int ans;
  64.  
  65.     ans = ((num % 2) == 0);
  66.     return (ans);  /*returns 1 if even, 0 if not */
  67.  
  68.  /* end even */
  69.  
  70.  
  71. Sorry about the formatting... the \ symbol is a backslash... for some 
  72. reason my comp doesn't wabt to read in the backslash.. :(
  73.  
  74.  
  75. Greg Roy
  76. Groy@drew.edu
  77.